package com.example.mvvmdemo.http.mtv;


import com.example.mvvmdemo.AppConstants;
import com.example.mvvmdemo.http.RetrofitBuilder;
import com.example.mvvmdemo.model.Mtv;
import com.example.mvvmdemo.model.PageInfo;
import com.example.mvvmdemo.model.mtvdetail.Movie;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class RetrofitHelper {

    private static RetrofitHelper mInstance;
    private static RetrofitHelper mInstance_url;
    private final RetrofitService mRetrofitService;

    private RetrofitHelper() {
        mRetrofitService = RetrofitBuilder.get().create(RetrofitService.class);
    }
    private RetrofitHelper(String url) {
        mRetrofitService = RetrofitBuilder.get(url).create(RetrofitService.class);
    }

    public static RetrofitHelper getInstance() {
        if (null == mInstance) {
            synchronized (RetrofitHelper.class) {
                if (null == mInstance) {
                    mInstance = new RetrofitHelper();
                }
            }
        }
        return mInstance;
    }

    public static RetrofitHelper getInstance(String url) {
        if (null == mInstance_url) {
            synchronized (RetrofitHelper.class) {
                if (null == mInstance_url) {
                    mInstance_url = new RetrofitHelper(url);
                }
            }
        }
        return mInstance_url;
    }

    public Observable<PageInfo<Mtv>> getMvtvList(String type, String tag, int page_limit, int page_start) {
        return mRetrofitService.getMvtvList(type, tag, page_limit, page_start)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io());
    }

    public Observable<Movie> getMvtvData(String id) {
        return mRetrofitService.getMvtvData(AppConstants.API_REFERER, id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io());
    }
}
